home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_13_11
/
phillip2
/
segments.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-06-29
|
64KB
|
2,212 lines
/***************************
*
* segments.c
* COMPOSITE FILE COMPRISING:
* segment.c
* segment2.c
*
***************************\
/*********************************************
*
* file d:\cips\segment.c
*
* Functions: This file contains
* adaptive_threshold_segmentation
* append_stack_files
* copy_stack_files
* find_peaks
* find_valley_point
* grow
* insert_into_peaks
* insert_into_deltas
* label_and_check_neighbors
* manual_threshold_segmentation
* peak_threshold_segmentation
* peaks_high_low
* push_data_onto_stack_file
* pop_data_off_of_stack_file
* threshold_image_array
* valley_high_low
* valley_threshold_segmentation
* get_segmentation_options
*
* Purpose:
* These functions are part of histogram
* based image segmentation.
*
* External Calls:
* wtiff.c - round_off_image_size
* create_file_if_needed
* write_array_into_tiff_image
* tiff.c - read_tiff_header
* rtiff.c - read_tiff_image
* numcvrt.c - get_integer
* get_short
*
* Modifications:
* October 1992 - created
*
************************************************/
#include "cips.h"
/**************************************************
*
* manual_threshold_segmentation(...
*
* This function segments an image using thresholding
* given the hi and low values of the threshold
* by the calling routine. It reads in an image
* and writes the result to the output image.
*
* If the segment parameter is 0, you only
* threshold the array - you do not segment.
*
***************************************************/
manual_threshold_segmentation(in_name, out_name,
the_image, out_image,
il, ie, ll, le,
hi, low, value, segment)
char in_name[], out_name[];
int il, ie, ll, le, segment;
short hi, low, the_image[ROWS][COLS],
out_image[ROWS][COLS], value;
{
int length, width;
struct tiff_header_struct image_header;
create_file_if_needed(in_name, out_name, out_image);
read_tiff_image(in_name, the_image, il, ie, ll, le);
threshold_image_array(the_image, out_image,
hi, low, value);
if(segment == 1)
grow(out_image, value);
write_array_into_tiff_image(out_name, out_image,
il, ie, ll, le);
} /* ends manual_threshold_segmentation */
/************************************************
*
* peak_threshold_segmentation(...
*
* This function segments an image using
* thresholding. It uses the histogram peaks
* to find the hi and low values of the
* threshold.
*
* If the segment parameter is 0, you only
* threshold the array - you do not segment.
*
*************************************************/
peak_threshold_segmentation(in_name, out_name,
the_image, out_image,
il, ie, ll, le,
value, segment)
char in_name[], out_name[];
int il, ie, ll, le, segment;
short the_image[ROWS][COLS],
out_image[ROWS][COLS], value;
{
int length, peak1, peak2, width;
short hi, low;
struct tiff_header_struct image_header;
unsigned long histogram[GRAY_LEVELS+1];
create_file_if_needed(in_name, out_name, out_image);
read_tiff_image(in_name, the_image, il, ie, ll, le);
zero_histogram(histogram);
calculate_histogram(the_image, histogram);
smooth_histogram(histogram);
find_peaks(histogram, &peak1, &peak2);
peaks_high_low(histogram, peak1, peak2,
&hi, &low);
threshold_image_array(the_image, out_image,
hi, low, value);
if(segment == 1)
grow(out_image, value);
write_array_into_tiff_image(out_name, out_image,
il, ie, ll, le);
} /* ends peak_threshold_segmentation */
/************************************************
*
* valley_threshold_segmentation(...
*
* This function segments an image using
* thresholding. It uses the histogram valleys
* to find the hi and low values of the
* threshold.
*
* If the segment parameter is 0, you only
* threshold the array - you do not segment.
*
*************************************************/
valley_threshold_segmentation(in_name, out_name,
the_image, out_image,
il, ie, ll, le,
value, segment)
char in_name[], out_name[];
int il, ie, ll, le, segment;
short the_image[ROWS][COLS],
out_image[ROWS][COLS], value;
{
int length, peak1, peak2, width;
short hi, low;
struct tiff_header_struct image_header;
unsigned long histogram[GRAY_LEVELS+1];
create_file_if_needed(in_name, out_name, out_image);
read_tiff_image(in_name, the_image, il, ie, ll, le);
zero_histogram(histogram);
calculate_histogram(the_image, histogram);
smooth_histogram(histogram);
find_peaks(histogram, &peak1, &peak2);
valley_high_low(histogram, peak1, peak2,
&hi, &low);
threshold_image_array(the_image, out_image,
hi, low, value);
if(segment == 1)
grow(out_image, value);
write_array_into_tiff_image(out_name, out_image,
il, ie, ll, le);
} /* ends valley_threshold_segmentation */
/************************************************
*
* adaptive_threshold_segmentation(...
*
* This function segments an image using
* thresholding. It uses two passes
* to find the hi and low values of the
* threshold. The first pass uses the peaks
* of the histogram to find the hi and low
* threshold values. It thresholds the image
* using these hi lows and calculates the means
* of the object and background. Then we use
* these means as new peaks to calculate new
* hi and low values. Finally, we threshold
* the image again using these second hi low
* hi low values.
*
* If the segment parameter is 0, you only
* threshold the array - you do not segment.
*
*************************************************/
adaptive_threshold_segmentation(in_name, out_name,
the_image, out_image,
il, ie, ll, le,
value, segment)
char in_name[], out_name[];
int il, ie, ll, le, segment;
short the_image[ROWS][COLS],
out_image[ROWS][COLS], value;
{
int length, peak1, peak2, width;
short background, hi, low, object;
struct tiff_header_struct image_header;
unsigned long histogram[GRAY_LEVELS+1];
create_file_if_needed(in_name, out_name, out_image);
read_tiff_image(in_name, the_image, il, ie, ll, le);
zero_histogram(histogram);
calculate_histogram(the_image, histogram);
smooth_histogram(histogram);
find_peaks(histogram, &peak1, &peak2);
peaks_high_low(histogram, peak1, peak2,
&hi, &low);
threshold_and_find_means(the_image, out_image,
hi, low, value,
&object, &background);
peaks_high_low(histogram, object, background,
&hi, &low);
threshold_image_array(the_image, out_image,
hi, low, value);
if(segment == 1)
grow(out_image, value)